import datetime
from django.db import models
from django.utils.translation import ugettext_lazy as _
from miles.db.managers import PublicableManager, DatePublicableManager


class PublicableModel(models.Model):
    is_public = models.BooleanField(_('is public?'), default=True)

    objects = models.Manager()
    live_objects = PublicableManager()

    def is_live(self):
        return self.is_public

    class Meta:
        abstract = True


class DatePublicableModel(models.Model):
    is_public = models.BooleanField(_('is public?'), default=True)
    pub_date = models.DateTimeField(_('publication date'), default=datetime.datetime.now)
    pub_end_date = models.DateTimeField(_('publication end date'), null=True, blank=True)

    objects = models.Manager()
    live_objects = DatePublicableManager()

    def is_live(self):
        if not self.is_public:
            return False
        now = datetime.datetime.now()
        in_range = self.pub_date <= now and (self.pub_end_date == None or self.pub_end_date > now)
        return in_range

    class Meta:
        abstract = True
